package com.shouhuobao.bhi.loc;

import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.LocationBean;

import droid.frame.activity.HandlerMgr;
import droid.frame.utils.android.Alert;

/**
 * 定位、然后查询地址<br>
 * 
 * @author coffee<br>
 *         2015-4-24上午10:29:17
 */
public class BaiduSearchService extends Service implements OnGetGeoCoderResultListener {

	private int msgId;

	private GeoCoder geoCoder;// 根据经纬度--反解析省市区
	// private PoiSearch poiSearch;// 根据关键字搜索

	private LocationBean currentLocation;// 当前位置
	private ArrayList<LocationBean> nearbyLocation;// 附近的位置

	@Override
	public void onCreate() {
		super.onCreate();
		geoCoder = GeoCoder.newInstance();
		geoCoder.setOnGetGeoCodeResultListener(this);
		//
		// poiSearch = PoiSearch.newInstance();
		// poiSearch.setOnGetPoiSearchResultListener(this);
	}

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			msgId = intent.getIntExtra("msgId", -1);
			double latitude = intent.getDoubleExtra("latitude", -1);
			double longitude = intent.getDoubleExtra("longitude", -1);
			if (latitude > 0 && longitude > 0) {
				LatLng ptCenter = new LatLng(latitude, longitude);
				geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 退出时销毁定位
		if (geoCoder != null) {
			geoCoder.destroy();
		}
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult arg0) {

	}

	/**
	 * 按照经纬苏搜索地址、结果列表
	 */
	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Alert.toast("抱歉，定位失败");
			return;
		}
		currentLocation = new LocationBean();
		currentLocation.setName1("当前位置");
		currentLocation.setAddress(result.getAddress());

		if (nearbyLocation == null) {
			nearbyLocation = new ArrayList<LocationBean>();
		} else {
			nearbyLocation.clear();
		}
		nearbyLocation.add(0, currentLocation);

		for (int i = 0; i < result.getPoiList().size(); i++) {
			PoiInfo poiInfo = result.getPoiList().get(i);
			LocationBean item = new LocationBean();
			item.setName1(poiInfo.name);
			item.setAddress(poiInfo.address);
			nearbyLocation.add(1 + i, item);
		}
		// 回调
		callback();
	}

	private void callback() {
		Message msg = Message.obtain();
		if (msgId == -1) {
			msg.what = MsgID.location_geocoder;// 反解析
		} else {
			msg.what = msgId;
			msgId = -1;// reset
		}
		msg.obj = nearbyLocation;
		HandlerMgr.sendMessage(msg, 0);
		// 重置元素
		currentLocation = null;
		nearbyLocation.clear();
		nearbyLocation = null;
		msgId = -1;
	}

}
